// 购物车模块
import axios from 'axios'
const cartModule = {
    namespaced: true,
    state: {
        list: [],
        br: false
    },
    mutations: {
        setList (state, newData) {
            state.list = newData
        },
        setAddNum (state, index) {
            state.list[index].goods_count++
        },
        setRedNum (state, index) {
            state.list[index].goods_count--
        },
        isAll (state) {
            state.br = !state.br
            state.list.forEach((item) => item.goods_state = state.br)
        },
        chenge (state, obj) {
            state.list[obj.index].goods_state = obj.st
            state.br = state.list.every((item) => item.goods_state)
        },
    },
    actions: {
        async setNewList (store) {
            let res = await axios({
                url: 'https://www.escook.cn/api/cart'
            })
            store.commit('setList', res.data.list)
        }
    },
    getters: {
        count (state) {
            return state.list.reduce((sum, item) => {
                if (item.goods_state) {
                    sum += item.goods_count
                }
                return sum
            }, 0)
        },
        countPrice (state) {
            return state.list.reduce((sum, item) => {
                if (item.goods_state) {
                    sum += item.goods_count * item.goods_price
                }
                return sum
            }, 0)
        },
    },
    modules: {}


}

export default cartModule